feat(db): file-per-migration system with up/down support for contributor QOL#265
Open
Tazrif-Raim wants to merge 1 commit into
Open
feat(db): file-per-migration system with up/down support for contributor QOL#265Tazrif-Raim wants to merge 1 commit into
Tazrif-Raim wants to merge 1 commit into
Conversation
3f710b0 to
711226f
Compare
eb8e653 to
3747b62
Compare
d93db5b to
1ffb2cb
Compare
…n system - introduce db/migrate/ with runner, CLI, and timestamp-named migration files - wrap entire V0-V24 legacy chain in 20260101_000000_legacy_baseline.ts - existing DBs detected via supports_tools sentinel, baseline skipped safely - add migrations tracking table for deterministic up/down execution - auto-run migrations on initDb() in all envs except NODE_ENV=development - add db:migration:up/down/fresh/status/create npm scripts - add round-trip CI test (up → down to baseline → up, schema must match) fix: legacy baseline migration should also run all the time fix: adjust catalog tests to use new migration
ac0ec7f to
93c7333
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problems
The project's entire migration history lived in a single db/migrations.ts (previously db/index.ts) file as a sequence of numbered functions (migrateModelsV1 through migrateModelsV24). This caused several compounding problems as the project grew:
Contributor conflicts: Two contributors working on separate features both needed to add migrateModelsV25. Even when their actual changes had nothing to do with each other, they were guaranteed a merge conflict or inconsistency in sequence on the function name and the call site in migrateDbSchema. Resolving it required manually renumbering and re-testing.
No way to undo local changes: When contributor B had written and applied a local migration, and contributor A's work landed on main first, B had no clean recovery path. The only options were manually writing compensating SQL directly in the SQLite file, or deleting the entire DB and losing all local API keys, settings, and request history. There was no down concept at all.
Migration file becoming too large: It's becoming increasingly harder to maintain.
As the project grows and contributors work asynchronously, these problems make DB changes increasingly painful to coordinate.
Description of changes
migrationstable20260101_000000_legacy_baseline.tshas changes in DB up until this PR.npm run db:migration:create --name=<new migration file name>(scaffold a new migration file)npm run db:migration:up(up to latest migration)npm run db:migration:down(roll back the latest migration)npm run db:migration:status(migration status)npm run db:migration:fresh(init fresh db; clear all data)Test
How to use
Applying migrations (first-time dev setup or after pulling changes that include new migrations in dev env):
i.e. when NODE_ENV=development, otherwise migrations run automatically to the latest
The contributor B conflict workflow:
How to create, write and up migration files dev workflow
Create a migration file
It will create a new migration file in
./server/src/db/migrationsEdit the scaffolding
Up the migration changes into DB
Dev Notes